﻿using System.Windows.Forms;
using DeviceUnion;

namespace DeviceUnion.Voice
{
    public partial class VoiceTestUserControl : UserControl
    {
        public VoiceHelper Helper { get; set; }

        public VoiceTestUserControl()
        {
            InitializeComponent();
            this.comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
            Helper = new VoiceHelper();
            this.Disposed += VoiceTestUserControl_Disposed;
        }

        private void VoiceTestUserControl_Disposed(object sender, System.EventArgs e)
        {
            Helper.Close();
        }

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, System.EventArgs e)
        {
            Helper.Close();
            Helper.InitVoice();
        }

        /// <summary>
        /// 刷新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, System.EventArgs e)
        {
            Helper.RefredVoices();
            this.numericUpDown1.Value = Helper.Rate;
            this.numericUpDown2.Value = Helper.Volume;
            this.comboBox1.Items.Clear();
            if (Helper.Voices != null && Helper.Voices.Count>0 )
            {
                Helper.Voices.ForEach(s => comboBox1.Items.Add(s));
                comboBox1.SelectedIndex = 0;
            }
        }

        /// <summary>
        /// 播放
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, System.EventArgs e)
        {
            if (!string.IsNullOrEmpty(comboBox1.Text))
            {
                Helper.ApplyVoice(comboBox1.Text);
            }
            Helper.Rate = (int)numericUpDown1.Value;
            Helper.Volume = (int)numericUpDown2.Value;

            if (!string.IsNullOrEmpty(this.textBox1.Text))
            {
                Helper.SpeakText(this.textBox1.Text);
            }
        }

    }
}